package com.myownthemepark.s3vn;

/**
 * Fetch the repository from S3 and place it in local storage.
 * 
 * @author alpha
 *
 */
public class FetchRepo {
	public static void main(String[] args){
		String svnZooBucket = null;
		String svnZooPrefix = null;
		String svnZooZKAddr = null;
		String svnZooZKPrefix = null;
		String repoPath = null;
		S3vn s3vn = null;
		boolean failure = false;

		if (args.length != 1){
			System.err.println("Usage: java FetchRepo <local repository path>");
			return;
		}
		
		try{
			svnZooBucket = System.getenv("SVNZOO_BUCKET");
			svnZooPrefix = System.getenv("SVNZOO_PREFIX");
			svnZooZKAddr = System.getenv("SVNZOO_ZKADDR");
			svnZooZKPrefix = System.getenv("SVNZOO_ZKPREFIX");
			
			repoPath = args[0];
			s3vn = new S3vn(repoPath, svnZooBucket, svnZooPrefix, svnZooZKAddr, svnZooZKPrefix);
			s3vn.setProgName("fetchrepo");
			
			s3vn.createZKSession();
			s3vn.createLocalRepo();
			s3vn.fetchRepo();
			s3vn.endZKSession();
		}catch (Exception e){
			System.err.println(String.format("[Error]:\n%s\n\n[Stack trace]:", e.toString()));
			StackTraceElement[] ste = e.getStackTrace();
			int i;
			for (i = 0; i < ste.length; i++){
				System.out.println(ste[i].toString());
			}
			failure = true;
		}finally{
			try{
				if (!failure)
					s3vn.endZKSession();
			}catch (Exception e){
				System.err.println(String.format("[Error]:\n%s\n\n[Stack trace]:", e.toString()));
				StackTraceElement[] ste = e.getStackTrace();
				int i;
				for (i = 0; i < ste.length; i++){
					System.out.println(ste[i].toString());
				}
			}
		}
	}
}
